// pages/shopManager/bannerManager/bannerEdit/bannerEdit.js
const common = require('../../../../utils/util.js')
const md5 = require('../../../../utils/md5.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        bannerId: null,
        bannerImg: '',
        bannerLink: '',
        bannerSort: 0,
        bannerShow: true,
        isLoading: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        if (options.bannerId) {
            this.setData({
                bannerId: options.bannerId
            })
            this.getBannerInfo()
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {
        let pages = getCurrentPages(); //获取当前页面实例
        let prepage = pages[pages.length - 2]
        if (prepage == undefined || prepage == null) return;
        prepage.updateData()
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    getBannerInfo: function() {
        wx.showLoading({
            title: '加载中'
        })
        const that = this
        let data = {
            store_id: wx.getStorageSync('storeId'),
            id: this.data.bannerId,
            client: 2
        }
        common.post('Slideshow/slideshow_list', data).then(function(res) {
            wx.hideLoading()
            if (res.code == 204) {
                return
            }
            that.setData({
                bannerImg: res[0].image,
                bannerLink: res[0].link,
                sort: res[0].sort,
                bannerShow: res[0].is_online == 1 ? true : false
            })
        }).catch(function(e) {})
    },

    bannerLinkInput: function(e) {
        this.setData({
            bannerLink: e.detail.value
        })
    },

    bannerSortInput: function(e) {
        this.setData({
            bannerSort: e.detail.value
        })
    },

    bannerShowChange: function(e) {
        this.setData({
            bannerShow: e.detail.value
        })
    },

    goSave: function() {
        if (this.data.isLoading) {
            return
        }
        if (this.data.bannerImg == '') {
            wx.showToast({
                title: '请上传首页轮播图',
                icon: 'none',
                duration: 2000
            })
            return
        }
        this.setData({
            isLoading: true
        })
        wx.showLoading({
            title: '保存中',
        })
        const that = this
        if (this.data.bannerId == null) {
            let data = {
                store_id: wx.getStorageSync('storeId'),
                image: this.data.bannerImg,
                link: this.data.bannerLink,
                is_online: this.data.bannerShow ? '1' : '0',
                sort: this.data.bannerSort,
                client: 2
            }
            common.post('Slideshow/add_slideshow', data).then(function(res) {
                wx.hideLoading()
                that.setData({
                    isLoading: false
                })
                if (res.code == 204) {
                    wx.showModal({
                        title: '提示',
                        content: res.error,
                        showCancel: false,
                        success(res) {}
                    })
                    return
                }
                wx.showModal({
                    title: '提示',
                    content: '新增轮播图成功',
                    showCancel: false,
                    success(res) {
                        if (res.confirm) {
                            wx.navigateBack({
                                delta: 1
                            })
                        }
                    }
                })
            }).catch(function(e) {})
        } else {
            let data = {
                id: this.data.bannerId,
                image: this.data.bannerImg,
                link: this.data.bannerLink,
                is_online: this.data.bannerShow ? '1' : '0',
                sort: this.data.bannerSort
            }
            common.post('Slideshow/up_slideshow', data).then(function(res) {
                wx.hideLoading()
                that.setData({
                    isLoading: false
                })
                if (res.code == 204) {
                    wx.showModal({
                        title: '提示',
                        content: res.error,
                        showCancel: false,
                        success(res) {}
                    })
                    return
                }
                wx.showModal({
                    title: '提示',
                    content: '更新轮播图成功',
                    showCancel: false,
                    success(res) {
                        if (res.confirm) {
                            wx.navigateBack({
                                delta: 1
                            })
                        }
                    }
                })
            }).catch(function(e) {})
        }
    },

    objKeySort: function(obj) {
        let newkey = Object.keys(obj).sort()
        let newObj = {}
        for (let i = 0; i < newkey.length; i++) {
            newObj[newkey[i]] = obj[newkey[i]]
        }
        return newObj
    },

    uploadImg: function() {
        const that = this
        let data = {}
        let key = wx.getStorageSync('token')
        let sign = 'ydy'
        let timestamp = Date.parse(new Date())
        data.key = key
        data.timestamp = timestamp
        data = this.objKeySort(data)
        for (let i in data) {
            sign += (i + data[i])
        }
        sign += 'ydy'
        sign = md5(sign)
        data.sign = sign
        wx.chooseImage({
            count: 1, // 默认9
            sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
            success: function(res) {
                wx.showLoading({
                    title: '正在上传图片',
                })
                let tempFilePaths = res.tempFilePaths
                wx.uploadFile({
                    url: 'https://yidianyuan.bailingyoupin.com/api/Upimg/up_comment_img', //仅为示例，非真实的接口地址
                    filePath: tempFilePaths[0],
                    name: 'file',
                    formData: {
                        key: data.key,
                        timestamp: data.timestamp,
                        sign: data.sign
                    },
                    success(res) {
                        if (JSON.parse(res.data).code == 204) {
                            wx.hideLoading()
                            wx.showModal({
                                title: '提示',
                                content: JSON.parse(res.data).error,
                                showCancel: false
                            })
                            return
                        }
                        wx.showToast({
                            title: '上传图片成功',
                            icon: 'none',
                            duration: 2000
                        })
                        that.setData({
                            bannerImg: JSON.parse(res.data).datas
                        })
                    }
                })
            }
        })
    }
})